package com.es.web.base.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.es.common.utils.StringUtils;
import com.es.web.base.code.Code;
import com.es.web.base.view.BaseView;

@Controller
public class BaseController {
	@Autowired
	private HttpServletRequest request;
	
	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public int getVersion(){
		String version = request.getHeader("version");
		version = version.replace(".", "");
		return StringUtils.toInt(version, 0);
	}
	public HttpSession getSession(){
		return request.getSession();
	}
	public boolean isLogin(){
		HttpSession session = getSession();
		Object obj = session.getAttribute(session.getId());
		if (obj != null) {
			return true;
		}
		return false;
	}
	public String render(Code code){
		BaseView view = new BaseView(code.getCode(), code.getMsg());
		return JSON.toJSONString(view);
	}
	
	public String render(Code code, Object obj){
		BaseView view = new BaseView(code.getCode(), code.getMsg(), obj);
		return JSON.toJSONString(view);
	}
	
	public String render(int code, String msg){
		BaseView view = new BaseView(code, msg);
		return JSON.toJSONString(view);
	}
	
	public String render(int code, String msg, Object obj){
		BaseView view = new BaseView(code, msg, obj);
		return JSON.toJSONString(view);
	}
}
